package Grasnador.Grasnador0.DTO;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import Grasnador.Grasnador0.Generic.GenericDTO;

/**
 * @author everton
 * 
 */

@Entity
@Table(name = "user")
public class User extends GenericDTO {
	private String name;
	private String email;
	private String username;
	private String password;
	private byte[] picture;

	@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinTable(name = "regrasnar", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "grasnado_id") })
	private Set<Grasnado> regrasnados = new HashSet<Grasnado>();
	
	@ManyToMany (fetch = FetchType.EAGER, mappedBy = "regrasnados")
	private Set<User> followers = new HashSet<User>();
	
	@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinTable(name = "follow", joinColumns = { @JoinColumn(name = "follower") }, inverseJoinColumns = { @JoinColumn (name = "followed") })
	private Set<User> follows = new HashSet<User>();

	public Set<User> getFollows() {
		return follows;
	}

	public void setFollows(Set<User> follows) {
		this.follows = follows;
	}

	public Set<User> getFollowers() {
		return followers;
	}

	public void setFollowers(Set<User> followers) {
		this.followers = followers;
	}

	public Set<Grasnado> getRegrasnados() {
		return regrasnados;
	}

	public void setRegrasnados(Set<Grasnado> regrasnados) {
		this.regrasnados = regrasnados;
	}

	@OneToMany
	@JoinColumn(name = "user_id")
	private List<Grasnado> grasnados;

	public List<Grasnado> getGrasnados() {
		return grasnados;
	}

	public void setGrasnados(List<Grasnado> grasnados) {
		this.grasnados = grasnados;
	}

	public byte[] getPicture() {
		return picture;
	}

	public void setPicture(byte[] picture) {
		this.picture = picture;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("User [name=");
		builder.append(name);
		builder.append(", email=");
		builder.append(email);
		builder.append(", username=");
		builder.append(username);
		builder.append(", password=");
		builder.append(password);
		builder.append(", picture=");
		builder.append(Arrays.toString(picture));
		builder.append(", regrasnados=");
		builder.append(regrasnados.size());
		builder.append(", followers=");
		builder.append(followers);
		builder.append(", follows=");
		builder.append(follows.size());
		builder.append(", grasnados=");
		builder.append(grasnados);
		builder.append(", id=");
		builder.append(id);
		builder.append("]");
		return builder.toString();
	}
}
